{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# numpy 高级索引\n",
    "# 除了之前看到的用整数和切片的索引外，数组可以由整数数组索引、布尔索引及花式索引。\n",
    "#\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "def aindex_01():\n",
    "    \"\"\"\n",
    "    整数数组索引\n",
    "    \"\"\"\n",
    "    print(\"整数数组索引\")\n",
    "\n",
    "    # 以下实例获取数组中 (0,0), (1,1) 和 (2,0) 位置处的元素\n",
    "    x = np.array([[1,  2],  [3,  4],  [5,  6]])\n",
    "    y = x[[0, 1, 2],  [0, 1, 0]]    # (0,0), (1,1) , (2,0) 位置\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print()\n",
    "\n",
    "    # 以下实例获取了 4X3 数组中的四个角的元素。\n",
    "    # 行索引是 [0,0] 和 [3,3]，而列索引是 [0,2] 和 [0,2]\n",
    "    # 得到 (0,0) (0,2) => [0,2]\n",
    "    #      (3,0) (3,2)=>  [9,11]\n",
    "    x = np.array([[0,  1,  2], [3,  4,  5], [6,  7,  8], [9,  10,  11]])\n",
    "    print('我们的数组是：')\n",
    "    print(x)\n",
    "    print()\n",
    "    rows = np.array([[0, 0], [3, 3]])   #\n",
    "    cols = np.array([[0, 2], [0, 2]])   #\n",
    "    y = x[rows, cols]\n",
    "    print('这个数组的四个角元素是：')\n",
    "    print(y)\n",
    "    print()\n",
    "\n",
    "    # 可以借助切片 : 或 … 与索引数组组合。如下面例子：\n",
    "    print('可以借助切片 : 或 … 与索引数组组合：')\n",
    "    a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "    b = a[1:3, 1:3]\n",
    "    c = a[1:3, [1, 2]]\n",
    "    d = a[..., 1:]\n",
    "    print(b)\n",
    "    print(c)\n",
    "    print(d)\n",
    "    return\n",
    "\n",
    "\n",
    "def aindex_02():\n",
    "    \"\"\"\n",
    "    布尔索引\n",
    "    我们可以通过一个布尔数组来索引目标数组。\n",
    "    布尔索引通过布尔运算（如：比较运算符）来获取符合指定条件的元素的数组。\n",
    "    \"\"\"\n",
    "    print(\"布尔索引\")\n",
    "    x = np.array([[0,  1,  2], [3,  4,  5], [6,  7,  8], [9,  10,  11]])\n",
    "    print('我们的数组是：')\n",
    "    print(x)\n",
    "    print()\n",
    "    print('大于 5 的元素是：')\n",
    "    print(x[x > 5])\n",
    "    print()\n",
    "\n",
    "    # 以下实例使用了 ~（取补运算符）来过滤 NaN。\n",
    "    a = np.array([np.nan,  1, 2, np.nan, 3, 4, 5])\n",
    "    print('我们的数组是：')\n",
    "    print(a)\n",
    "    print('使用了~来过滤NaN：')\n",
    "    print(a[~np.isnan(a)])\n",
    "    print()\n",
    "\n",
    "    # 从数组中过滤掉非复数元素\n",
    "    a = np.array([1,  2+6j,  5,  3.5+5j])\n",
    "    print('我们的数组是：')\n",
    "    print(a)\n",
    "    print('从数组中过滤掉非复数元素: ')\n",
    "    print(a[np.iscomplex(a)])\n",
    "    print()\n",
    "    return\n",
    "\n",
    "\n",
    "# aindex_01()\n",
    "aindex_02()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.7 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "60d0b561bcf179bd0cd76361af4313e00281c3c5daca1903769e6cc3dd82b37c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
